<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    
</body>
<script>

    // ajax({
    //     url:"http://localhost:3000/getGoods"
    // }).then(res=>{
    //     console.log(res);
    // })

    function ajax({url="", type="get", data={}, timeout=10000}={}){
        let str = "";
        for(let i in data){
            str += `${i}=${data[i]}&`;
        }
        url = type==="get" ? url+"?"+str.slice(0,-1) : url;

        const xhr = new XMLHttpRequest();
        xhr.open(type, url);
        xhr.timeout = timeout;
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send(type==="post" ? str.slice(0,-1) : undefined);
        return new Promise((resolve, reject)=>{
            xhr.onload = function(){
                if(xhr.status === 200){
                    resolve(xhr.responseText);
                }else{
                    reject(xhr.status);
                }
            }
            xhr.ontimeout = function(){
                reject("timeout");
                xhr.abort();
            }
        })
    }
    
</script>
</html>